package com.sprider.jsoup.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.sprider.jsoup.service.SpriderAbstractService;

public class InfoListSpriderService extends SpriderAbstractService {
	private List<String> getDomainList(Document doc){
		Elements els = doc.select(".lmc-list a");
		Iterator<Element> it = els.iterator();
		List<String> list = new ArrayList<String>();
		while (it.hasNext()) {
			Element el = it.next();
			String href = el.attr("href");
			list.add(href);
		}
		
		return list;
	}
	@Override
	protected Object parse() throws IOException {
		List<String> list=new ArrayList<String>();
		list.addAll(this.getDomainList(this.doc));
		Integer pn = null;
		Elements pnEls = this.doc.select(".pg strong").next();
		
		Iterator<Element> pnEl = pnEls.iterator();
		if (pnEl.hasNext()) {
			pn = new Integer(pnEl.next().html()) ;

		}
		while (true) {
			if (pn == null) {
				break;
			}
			Connection c = Jsoup.connect(this.url + "?pn=" + pn);
			System.out.println(pn);
			Document d = c.get();
			list.addAll(this.getDomainList(d));
			pnEls = d.select(".pn strong").next();
			 pnEl = pnEls.iterator();
			 System.out.println(pnEls);
			if (pnEl.hasNext()) {
				pn = new Integer(pnEl.next().html())-1;
			}else{
				pn=null;
			}
			
		}
		return list;
	}

}
